<?php
/**
 * Created by PhpStorm.
 * User: keke
 * Date: 2020/8/11
 * Time: 21:03
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Model\Hint;
use App\Lib\Response;
use App\Http\Services\FinanceUnionServices;

class FinanceUnionController extends BaseController
{
    protected $financeUnionSetting;

    public function __construct()
    {
        $this->financeUnionSetting = new FinanceUnionServices();
    }


    /**
     * 检索用户信息
     * @param Request $request
     */
    public function getAppointUsers(Request $request)
    {
        $uuid = $request->get('uuid_id');
        $unionId = $request->get('union_id');
        if (empty($uuid) || empty($unionId)) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        $result = $this->financeUnionSetting->getUserInfoByUserUid(intval($uuid), intval($unionId));

        if ($result == Hint::LOGIN_OUT) {
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($result == Hint::USER_NOT_EXIST) {
            Response::returnResult(Hint::USER_NOT_EXIST);
            return;
        }
        if ($result == Hint::FINANCE_UNION_USER_NOT_BIND) {
            Response::returnResult(Hint::FINANCE_UNION_USER_NOT_BIND);
            return;
        }
        if ($result == Hint::FINANCE_UNION_USER_NOT_EXIST) {
            Response::returnResult(Hint::FINANCE_UNION_USER_NOT_EXIST);
            return;
        }
        if ($result == Hint::USER_FORBID) {
            Response::returnResult(Hint::USER_FORBID);
            return;
        }
        Response::returnResult(Hint::SUCCESS, $result);
    }


    /**
     * 指定结算用户
     * @param Request $request
     */
    public function setSettleUser(Request $request)
    {
        $unionId = $request->get('union_id');
        $settleUserId = $request->get('settle_user_id');

        if (empty($unionId) || empty($settleUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        $result = $this->financeUnionSetting->setSettleUser($unionId, $settleUserId);
        if ($result == Hint::LOGIN_OUT) {
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($result == Hint::AUTH_ERROR) {
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        if ($result == Hint::NOT_ALLOWED) {
            Response::returnResult(Hint::NOT_ALLOWED);
            return;
        }
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 解绑结算用户/提现用户
     * @param Request $request
     */
    public function unbindUser(Request $request)
    {
        $unionId = $request->get('union_id');
        $type = $request->get('type') ?? 0;
        if (empty($unionId)) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        $result = $this->financeUnionSetting->unbindUser($unionId, intval($type));
        if ($result == Hint::LOGIN_OUT) {
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($result == Hint::AUTH_ERROR) {
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 指定提现用户
     * @param Request $request
     */
    public function setWithdrawUser(Request $request)
    {
        $unionId = $request->get('union_id');
        $withdrawUserId = $request->get('withdraw_user_id');

        if (empty($unionId) || empty($withdrawUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        $result = $this->financeUnionSetting->setWithdrawUser($unionId, $withdrawUserId);
        if ($result == Hint::LOGIN_OUT) {
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($result == Hint::AUTH_ERROR) {
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        if ($result == Hint::NOT_ALLOWED) {
            Response::returnResult(Hint::NOT_ALLOWED);
            return;
        }
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 获取公会指定结算用户/提现用户基本信息
     * @param Request $request
     */
    public function getAppointUserInfo(Request $request)
    {
        $unionId = $request->get('union_id');
        $type = $request->get('type') ?? 0;
        if (empty($unionId)) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }
        $result = $this->financeUnionSetting->getAppointUserInfo($unionId, intval($type));
        Response::returnResult(Hint::SUCCESS, $result);
    }


    /**
     * 获取绑定记录
     * @param Request $request
     */
    public function getBindRecords(Request $request)
    {
        $unionId = $request->get('union_id');
        if (empty($unionId)) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        $result = $this->financeUnionSetting->getBindRecords($unionId);
        if ($result == Hint::LOGIN_OUT) {
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        if ($result == Hint::AUTH_ERROR) {
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        Response::returnResult(Hint::SUCCESS, $result);
    }
}